package sorter;

import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;

public class Quicksort extends UnicastRemoteObject implements ISorter 
{
        Comparable[] workArray;
        Quicksort() throws RemoteException
        {
        }

        public Comparable[] sort(Comparable[] inputArray)
        {
                workArray = inputArray;
                quickSort(0, inputArray.length - 1 );
                return workArray;
        }
        
        public void quickSort (int lowerIndex, int higherIndex)
        {
            int l=lowerIndex, h=higherIndex;
            Comparable pivot=workArray[(lowerIndex+higherIndex)/2];
            do
            {    
                while (workArray[l].compareTo(pivot)<0) l++; 
                while (workArray[h].compareTo(pivot)>0) h--;
                if (l<=h)
                {
                        swapObject(l,h);
                    l++; 
                    h--;
                }
            }
            while (l<=h);
                if (lowerIndex<h) quickSort(lowerIndex, h);
                if (l<higherIndex) quickSort(l, higherIndex);
        }

        private void swapObject(int lowerIndex, int higherIndex) {
                Comparable tempObject=workArray[lowerIndex]; 
        workArray[lowerIndex]=workArray[higherIndex]; 
        workArray[higherIndex]=tempObject;
        }
}

